/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.eb.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.eb.entity.ProductClassify;
import com.kaaction.modules.eb.service.ProductClassifyService;

/**
 * 商品与分类Controller
 * @author zengbiao
 * @version 2017-04-19
 */
@Controller
@RequestMapping(value = "${adminPath}/eb/productClassify")
public class ProductClassifyController extends BaseController {

	@Autowired
	private ProductClassifyService productClassifyService;
	
	@ModelAttribute
	public ProductClassify get(@RequestParam(required=false) String id) {
		ProductClassify entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = productClassifyService.get(id);
		}
		if (entity == null){
			entity = new ProductClassify();
		}
		return entity;
	}
	
	@RequiresPermissions("eb:productClassify:view")
	@RequestMapping(value = {"list", ""})
	public String list(ProductClassify productClassify, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<ProductClassify> page = productClassifyService.findPage(new Page<ProductClassify>(request, response), productClassify); 
		model.addAttribute("page", page);
		return "modules/eb/productClassifyList";
	}

	@RequiresPermissions("eb:productClassify:view")
	@RequestMapping(value = "form")
	public String form(ProductClassify productClassify, Model model) {
		model.addAttribute("productClassify", productClassify);
		return "modules/eb/productClassifyForm";
	}

	@RequiresPermissions("eb:productClassify:edit")
	@RequestMapping(value = "save")
	public String save(ProductClassify productClassify, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, productClassify)){
			return form(productClassify, model);
		}
		productClassifyService.save(productClassify);
		addMessage(redirectAttributes, "保存商品与分类成功");
		return "redirect:"+Global.getAdminPath()+"/eb/productClassify/?repage";
	}
	
	@RequiresPermissions("eb:productClassify:edit")
	@RequestMapping(value = "delete")
	public String delete(ProductClassify productClassify, RedirectAttributes redirectAttributes) {
		productClassifyService.delete(productClassify);
		addMessage(redirectAttributes, "删除商品与分类成功");
		return "redirect:"+Global.getAdminPath()+"/eb/productClassify/?repage";
	}

}